import { Router } from 'express'
import { ObjectId } from 'mongodb'
import _ from 'lodash'
import msg from '../msg/message.js'
import { DataBase } from '../utils/database/mongodb/database'

const router = Router()
let db = new DataBase()
db.initMongoDB()
db.setDatabase('yd')

router.post('/set/:database/', (req, res) => {
  db.setDatabase(req.params.database)
  res.json(msg.success)
})

// 新增
router.post('/add/:collection/', (req, res) => {
  req.body._id = ObjectId(req.body._id)
  db.insert(req.params.collection, req.body, doc => {
    res.json(req.body._id)
  })
})
// 更新
router.post('/update/:collection/', (req, res) => {
  // console.log('add->')
  // console.log(req)
  req.body._id = ObjectId(req.body._id) // 必须转ObjectId
  db.updateOne(req.params.collection, { _id: req.body._id }, req.body, doc => {
    res.json(doc)
  })
})

// 查询
// http://localhost:3000/api/get/collection/
router.get('/get/:collection/', function (req, res, next) {
  let filter = req.query
  // console.log(filter)
  db.find(req.params.collection, filter, { _id: -1 }, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 查询
// http://localhost:3000/api/get/collection/
router.get('/getAccounts/', function (req, res, next) {
  let filter = req.query
  // console.log(filter)
  db.find('prisonerAccountItem', filter, { _id: -1 }, acc => {
    if (!acc) {
      res.json(msg.noFound)
    } else {
      db.find('prisoners', {}, { _id: -1 }, pri => {
        _.each(acc, obj => {
          let prisoner = _.find(pri, o => {
            return o._id.toString() === obj.prisonerId
          })
          obj = _.assign(obj, _.pick(prisoner, ['code', 'name', 'prison']))
        })
        res.json(acc)
      })
    }
  })
})

// 查询
// http://localhost:3000/api/getUserList
router.get('/getUserList', function (req, res, next) {
  db.find('users', {}, { _id: -1 }, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      let n = []
      _.forEach(doc, value => {
        n.push(_.pick(value, ['_id', 'username', 'realName', 'email']))
      })
      res.json(n)
    }
  })
})

// 查询
// http://localhost:3000/api/get/gonggao/?key=value&key1=value1
// {_id: -1} _id ObjectId 倒序排序
router.get('/get/:collection/:skip/:limit', function (req, res, next) {
  let filter = req.query
  // console.log(filter)
  db.find(req.params.collection, filter, { _id: -1 }, parseInt(req.params.skip), parseInt(req.params.limit), doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 查询 去掉content和summary，减少数据量
// http://localhost:3000/api/get/news/?key=value&key1=value1
// {_id: -1} _id ObjectId 倒序排序
router.get('/find/:collection/:skip/:limit', function (req, res, next) {
  let filter = req.query
  // console.log(filter)
  db.find(req.params.collection, filter, { _id: -1 }, parseInt(req.params.skip), parseInt(req.params.limit), {
    content: 0,
    summary: 0,
    img: 0
  }, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 查询
// http://localhost:3000/api/get/news/?key=value&key1=value1
// {_id: -1} _id ObjectId 倒序排序
router.post('/find/:collection/:skip/:limit', function (req, res, next) {
  let filter = req.query
  let project = req.body
  // console.log(filter)
  db.find(req.params.collection, filter, { _id: -1 }, parseInt(req.params.skip), parseInt(req.params.limit), project, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 取头条，数量:limit
router.get('/top/:collection/:limit', function (req, res, next) {
  let filter = { column: '头条' }
  db.find(req.params.collection, filter, { _id: -1 }, 0, parseInt(req.params.limit), { content: 0 }, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 查询数量
router.get('/count/:collection/', function (req, res, next) {
  let filter = req.query
  db.count(req.params.collection, filter, n => {
    // console.log(n)
    res.json({ number: n })
  })
})

// 查询
// http://localhost:3000/api/get/gonggao/key/value
router.get('/get/:collection/:key/:value', function (req, res, next) {
  let filter = {}
  let key = req.params.key
  filter[key] = req.params.value
  // console.log(filter)
  db.findOne(req.params.collection, filter, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 按id查询
router.get('/get/:collection/:id', function (req, res, next) {
  db.findOne(req.params.collection, { _id: ObjectId(req.params.id) }, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})

// 删除
router.delete('/delete/:collection/:id', function (req, res) {
  db.deleteOne(req.params.collection, { _id: ObjectId(req.params.id) }, doc => {
    if (!doc) {
      res.json(msg.noFound)
    } else {
      res.json(doc)
    }
  })
})
export default router
